<template>
    <div o-Container>
        <!-- 班级群管理 -->
        <o-list
            v-bind:source="list.dictionary"
            v-bind="{ created: create, modified: modify }"
        />
        <o-dialog v-bind:source="panel" v-bind="{ opened: load, closed: load }">
            <template slot="content">
                <o-item
                    v-bind:source="item.dictionary"
                    v-bind="{
                        saved: save,
                        backed: back,
                        changed: change,
                        validated: validate,
                    }"
                ></o-item>
            </template>
        </o-dialog>
    </div>
</template>

<script>
export default {
    name: "mms_data_dictionary_list" /* 这个name暂时不知道用啥用，根据官方文档说的是方便排错的 */,
    data() {
        return {
            octopus: this.$parent.octopus,
            // 获取班级群列表
            list: {
                dictionary: {
                    data: {
                        id: 400, // 菜单表的八爪鱼ID
                        cellData: [
                            //  { name: "PhotoData" }, //头像
                            { name: "BerthID", searchKey: "like", width: 50 }, //班级
                            {
                                name: "BerthName",
                                searchKey: "like",
                                width: 100,
                            }, //班级
                            // { name: "CreateDate" }, //创建时间时间
                            // { name: "QRCode"}, //二维码
                            { name: "Name", searchKey: "like", width: 100 }, //名称
                            { name: "ID", searchKey: "like", width: 100 }, //id
                            { name: "LastSenderDate", width: 200 }, //时间
                            { name: "LastSenderName", width: 200 }, //最后发言者
                            { name: "LastSenderContent", width: 200 }, //消息
                            { name: "UserID", width: 100 }, //,filterKey: "=", filterValue: this.octopus.passport.id
                        ],
                    },
                },
            },
            // 获取班级群列表
            item: {
                dictionary: {
                    // 以下参数默认由组件内部创建，但是如果有需要，开发人员可在这里提前设置，组件将按照您的预期渲染。
                    // cache: false,
                    // control: null,
                    // dataExtend: null,
                    data: {
                        id: 400,
                        value: "",
                        cellData: [
                            //  { name: "PhotoData" }, //头像
                            { name: "BerthID", width: 70 }, //班级ID
                            {
                                name: "BerthName",
                                searchKey: "like",
                                width: 100,
                            }, //班级
                            { name: "CreateDate" }, //创建时间时间
                            // { name: "QRCode"}, //二维码
                            { name: "Name" }, //名称
                            { name: "ID" }, //id
                            { name: "LastSenderDate", width: 200 }, //时间
                            { name: "LastSenderName" }, //最后发言时间
                            { name: "LastSenderContent" }, //最后发的消息
                            { name: "UserID" },
                        ],
                    },
                },
            },
            panel: {
                visible: false,
            },
            KeyValueArr: [],
        };
    },
    methods: {
        create: function () {
            this.item.dictionary.data.value = null;
            this.item.dictionary.control.load(); //刷新
            this.panel.visible = true; //打开
        },
        modify: function (row) {
            // 加载对应的列关联绑定数据
            this.item.dictionary.data.value = row.ID;
            this.item.dictionary.control.load();
            this.panel.visible = true; //打开
        },
        validate(status) {
            if (status) {
                //默认校验通过
                if (this.KeyValueArr.length > 0) {
                    for (let s of this.KeyValueArr) {
                        if (
                            s.Code ==
                            this.octopus.tools.parseCell(
                                this.item.dictionary.data,
                                "Code"
                            ).defaultValue
                        ) {
                            //同一个Code，不能有相同的KeyValue
                            this.octopus.tips.error("键已存在，请重新输入");
                            return false;
                        }
                    }
                }
            }
            return status;
        },
        change(data) {
            //同一个Code，不能有相同的KeyValue
            if (data.name == "KeyValue") {
                // if(this.initCode==data.defaultValue){//编辑初始值 和 后来改变值相等；说明编辑时候没有改变Code字段的值，所以不用查询了
                //   this.hasCode=false;
                //   return
                // }
                this.octopus.request.api.post(
                    "/Octopus/Engine/List",
                    {
                        id: 183,
                        cellData: [
                            {
                                name: "Code",
                            },
                            {
                                name: "KeyValue",
                                filterKey: "=",
                                filterValue: data.defaultValue,
                            },
                        ],
                    },
                    (data) => {
                        if (
                            data.status.code == 200 &&
                            data.content.queryData.length > 0
                        ) {
                            this.KeyValueArr = data.content.queryData;
                        } else {
                            this.KeyValueArr = [];
                        }
                    }
                );
            }
        },
        save: function () {
            //同一个Code，不能有相同的KeyValue
            // 刷新list
            this.list.dictionary.control.load(); //刷新
            this.back(); //返回
        },
        back: function () {
            this.panel.visible = false; //取消
        },
    },
    props: ["oData"], //数据
};
</script>